<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use fast\Http;
use app\common\library\Faceid as FaceIdLib;
use app\admin\model\pt\Person as PersonModel;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }

    public function faceidResult($uid)
    {
        $personModel = PersonModel::where('user_id', $uid)->find();
        if (!$personModel) {
            echo '未获取到人员信息';
            exit;
        }

        $bizToken = $this->request->get('BizToken');
        if (!$bizToken) {
            echo 'no token';
            exit;
        }

        $faceidLib = new FaceIdLib();
        $checkRes = $faceidLib->getDetectInfo($bizToken);

        $msg = $checkRes['msg'];
        $data = $checkRes['data'];

        if ($msg != 'ok') {
            $this->error($msg, $data);
        }
        $textRes = $data['Text'];
        if ($textRes['Comparemsg'] == '成功') {
            // 判断是否与提交的姓名和身份证号一致
            if ($textRes['IdCard'] != $personModel->id_card || $textRes['Name'] != $personModel->user_name) {
                $personModel->confirm_idcard = 2;
                $personModel->confirm_desc = '第三方活体认证结果与输入的姓名或身份证不一致';
                $msg = '失败';
            } else {
                $personModel->confirm_idcard = 1;
                $personModel->confirm_desc = '认证成功';
                $personModel->confirm_time = time();
                $msg = '成功';
            }
        } else {
            $personModel->confirm_idcard = 2;
            $personModel->confirm_desc = $textRes['ErrMsg'];
            $msg = '失败，错误原因：'.$textRes['ErrMsg'];
        }
        $personModel->confirm_end_time = 0;
        $personModel->save();


        $this->view->msg = $msg;
        return $this->view->fetch();
    }

}
